// ----------------
// Project:
// ESA Elevator
// ----------------
//
// ----------------
// Group: 163
// 2358569: Pascal Klink
// 2649456: Lukas Kreh
// ----------------
//
// Description:
// ----------------
// elevator keyboard-interface
//
// Version History:
// ----------------
// 140116: set parameter FLOORS to 30
// 140116: set parameter FLOOR_BITS to 5

`timescale 1ns / 1ns

module keyboard_if

          #(parameter FLOORS     = 30,
                      FLOOR_BITS =  5)


          (input  wire                    CLK,
           input  wire                    RESET,
           
           input  wire [(FLOORS-1)    :0] FLOOR_SELECT,             
           input  wire                    CLOSE_DOOR_IN,            
           input  wire                    OPEN_DOOR_IN,             
           input  wire                    PASSENGER_ALARM_IN,       
           input  wire [(FLOOR_BITS-1):0] CLEAR_FLOOR_BUTTON,       // reset_button
           input  wire                    CLEAR_FLOOR_BUTTON_VALID, 
            
           output wire                    CLOSE_DOOR_OUT,           // close
			  output wire			  				OPEN_DOOR_OUT,	    // open
           output wire                    PASSENGER_ALARM_OUT,      // alarm        
           output wire [(FLOORS-1)    :0] SELECTED_FLOORS,          // floor
           output wire [(FLOORS-1)    :0] ENLIGHT_BUTTONS);         // enlight
			  
/* =============================INSERT CODE HERE======================================*/ 

//regs
reg close_door;
reg open_door;
reg passenger_alarm;
reg[(FLOORS-1):0] floors_selected;

//assign values
assign CLOSE_DOOR_OUT=close_door;
assign OPEN_DOOR_OUT=open_door;
assign PASSENGER_ALARM_OUT=passenger_alarm;
assign SELECTED_FLOORS=floors_selected;
assign ENLIGHT_BUTTONS=floors_selected;

always@(posedge CLK)begin
	//reset everything... 
	if(RESET)begin
		floors_selected[(FLOORS-1):0]<=0;
		close_door<=0;
		open_door<=0;
		passenger_alarm<=0;
	end
	else begin 
		//door close and set open_door to 0
		if(close_door!=CLOSE_DOOR_IN)begin
			close_door<=CLOSE_DOOR_IN; 
			if(close_door)begin
				open_door<=0;
			end
		end
		//door open and set close_door to 0
		if(open_door!=OPEN_DOOR_IN)begin
			open_door<=OPEN_DOOR_IN;
			if(open_door)begin
				close_door<=0;
			end
		end
		//passenger alarm
		if(passenger_alarm!=PASSENGER_ALARM_IN)begin
			passenger_alarm<=PASSENGER_ALARM_IN;
		end		
		//selected Floors pipeline
		if(FLOOR_SELECT[(FLOORS-1):0]!=0)begin
			//update selected Floors via OR
			floors_selected<=floors_selected | FLOOR_SELECT;
		end
		//oly clean if CLEAR_FLOOR_BUTTON_VALID is 1
		if(CLEAR_FLOOR_BUTTON_VALID)begin
			floors_selected[CLEAR_FLOOR_BUTTON]<=0;	//set the floors bit to 0
		end
	end
end
/* ====================================================================================*/
endmodule